<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;
use think\helper\Time;

class Project extends Base
{
    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                if($this->uid){
                    session('uid', $this->uid);
                }else{
                    $url=url('/wx/interest/index' , '', '', Request::host());
                    $this->redirect($url);
                }
            }
        } else {
            $url = url('project/modelindex?model_id=' . input('model_id'), '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }

    public function modelindex()
    {
        $this->assign('model_id', input('model_id'));
        $project_model = config('param.project_model');
        $this->assign('model_name', $project_model[input('model_id')]);
        $this->assign('model_id', input('model_id'));
        return view();

    }

    public function doc()
    {
        return view();
    }

    public function project_list($uid)
    {

        $list = DB::view('project', 'id,title')
            ->view('project_user', 'uid,project_id', 'project.id=project_user.project_id')
            ->where('project_user.uid', $uid)
            ->select();
        return implode(',', array_column($list, 'id'));

    }


    public function doc_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        // dump($this->project_list(session('uid')));exit;
        $map[] = ['doc.project_id', 'in', $this->project_list(session('uid'))];
        $map[] = ['doc.delete_time', '=', 0];
        $data['count'] = Db::view('doc', 'id,project_id,title,c_time,uid,doc_type,doc_view')
            //  ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'doc.uid=adminuid.id', 'LEFT')
            // ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'doc.uid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'doc.project_id=project.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('doc', 'id,project_id,title,c_time,uid,doc_type,doc_view')
            // ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'doc.uid=adminuid.id', 'LEFT')
            // ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'doc.touid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'doc.project_id=project.id')
            ->where($map)
            //->fetchSql(true)
            ->order(['status', 'id' => 'desc',])->page(input("param.page"), input('param.limit'))->select();
        //echo $data['data'];

        foreach ($data['data'] as &$info) {
            $info['title'] = $info['project_title'] . '#' . $info['title'];
            $info['c_time'] == 0 ? $info['c_time'] = '--' : $info['c_time'] = date('m-d', $info['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function model_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['stories.delete_time', '=', 0];
        $map[] = ['stories.uid|stories.touid', '=', session('uid')];
        $map[] = ['stories.status', '<', 4];
        $map[] = ['stories.model_id', '=', $post['model_id']];
        $data['count'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.uid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.touid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            //->fetchSql(true)
            ->order(['status', 'id' => 'desc',])->page(input("param.page"), input('param.limit'))->select();
        //echo $data['data'];

        $stories_status = config('param.model_' . $post['model_id'] . '_status');
        $level_status = config('param.level_status');
        foreach ($data['data'] as &$info) {
            $info['status'] = $stories_status[$info['status']];
            $info['level_id'] = $level_status[$info['level_id']];
            $info['title'] = $info['project_title'] . '#' . $info['title'];
            $info['s_time'] == 0 ? $info['s_time'] = '--' : $info['s_time'] = date('m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '--' : $info['e_time'] = date('m-d', $info['e_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    //临期
    public function workindex()
    {
        return view();
    }

    public function work_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->get();


        $map = array();
        $map[] = ['stories.uid|stories.touid', '=', session('uid')];
        $map[] = ['stories.delete_time', '=', 0];
        $map[] = ['stories.status', '<>', 5];
        $data['data'] = Db::view('stories', 'id,project_id,title,touid,e_time,uid,p_time,status,delete_time,model_id')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('e_time asc')
            ->page(1, 15)
            ->select();
        $project_model = config('param.project_model');
        foreach ($data['data'] as &$info) {
            $info['title'] = $info['project_title'] . '#' . $info['title'];
            $info['e_time'] = date('m-d', $info['e_time']);
            $info['model_id'] = $project_model[$info['model_id']];
        }
        $data['count'] = 15;
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }
}
